package com.education.wisdom.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.CatalogLive;
import com.education.wisdom.pojo.JrMoments;
import com.education.wisdom.pojo.JrUserConent;
import com.education.wisdom.service.ICatalogLiveService;
import com.education.wisdom.service.IJrMomentsService;
import com.education.wisdom.service.JrUserConentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 朋友圈 信息操作处理
 *
 * @author d
 * @date 2020-07-07
 */
@RestController
@RequestMapping("/web/jrMoments")
public class FullMomentsController {

    @Autowired
    private IJrMomentsService momentsService;

    @Autowired
    private ICatalogLiveService catalogLiveService;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private JrUserConentService userConentService;

    @Autowired
    JrUserConentService jrUserConentService;


    /**
     * 新增
     * @param catalogLive
     * @return
     */
    @PostMapping("/insert")
    public ApiReturnObject insert(@RequestBody CatalogLive catalogLive){
        catalogLive.setCatalogCode(UUIDUtils.getByUUId());
        catalogLive.setCreateTime(DateUtil.now());
        int i = catalogLiveService.insertCatalogLive(catalogLive);
        if(i > 0) return new ApiReturnObject("200", "success", null);
        return new ApiReturnObject("201", "warning", null);
    }


    /**
     * 删除
     * @param catalogId
     * @return
     */
    @GetMapping("/delete")
    public ApiReturnObject delete(String catalogId){
        int i = catalogLiveService.deleteCatalogLiveByIds(catalogId);
        if(i > 0) return new ApiReturnObject("200", "success", null);
        return new ApiReturnObject("201", "warning", null);
    }

    /**
     * 修改
     * @param catalogLive
     * @return
     */
    @PostMapping("/update")
    public ApiReturnObject update(@RequestBody CatalogLive catalogLive){
        int i = catalogLiveService.updateCatalogLive(catalogLive);
        if(i > 0) return new ApiReturnObject("200", "success", null);
        return new ApiReturnObject("201", "warning", null);
    }

    /**
     * 查询
     * @param
     * @return
     */
    @GetMapping("/searchMenu")
    public ApiReturnObject searchMenu(Integer pageNum,Integer pageSize,CatalogLive catalogLive){
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        catalogLive.setName(catalogLive.getName());
        List<CatalogLive> catalogLives = catalogLiveService.selectCatalogLiveListlikename(catalogLive);
        PageInfo<CatalogLive> info = new PageInfo<>(catalogLives);
        return new ApiReturnObject("200", "success", info);
    }
    

    /**
     * 搜索 & 查询全部
     */
    @GetMapping("/search")
    public ApiReturnObject search(JrMoments moments, Integer pageNum, Integer pageSize,Integer usId) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        moments.setDeflag("2");
        List<JrMoments> jrMoments = momentsService.selectJrMomentsList(moments);
        for (JrMoments jrMoment : jrMoments) {
            Map<String, Object> map = userFeign.selectByUserId(Integer.parseInt(jrMoment.getUserId()));
            jrMoment.setSysIco((String) map.get("image"));
            jrMoment.setSysRelName((String) map.get("name"));
            if(jrMoment.getImages() != null){
                String[] split = jrMoment.getImages().split(",");
                jrMoment.setImageList(split);
            }else {
                jrMoment.setImageList(new String[1]);
            }
            //查询该资源是否收藏
            if (usId != null) {
                JrUserConent conent = new JrUserConent();
                conent.setUserId(usId);
                conent.setComentId(jrMoment.getId());
                List<JrUserConent> conentList = userConentService.selectJrUserConentList(conent);
                if (conentList.size() > 0){
                    jrMoment.setIsRed("1");//已收藏
                }else {
                    jrMoment.setIsRed("0");//未收藏
                }
            }
        }
        PageInfo<JrMoments> info = new PageInfo<>(jrMoments);
        return new ApiReturnObject("200", "操作成功", info);
    }

    /**
     * 达人直播课程详情
     */
    @GetMapping("/detail")
    public ApiReturnObject detail(Integer id) {

        JrMoments jrMoments = momentsService.selectJrMomentsById(id);
        JrUserConent jrUserConent = new JrUserConent();
        jrUserConent.setComentId(jrMoments.getId());
        Map<String, Object> stringObjectMap = userFeign.selectByUserId(Integer.parseInt(jrMoments.getUserId()));
        jrMoments.setUserName(stringObjectMap.get("name") + "");
        jrMoments.setUserIco(stringObjectMap.get("image") + "");
        List<JrUserConent> jrUserConents = userConentService.selectJrUserConentList(jrUserConent);
        if(jrUserConents.size()!=0) {
            jrMoments.setIsLike("1");
        }else{
            jrMoments.setIsLike("0");
        }
        return new ApiReturnObject("200", "操作成功", jrMoments);
    }

    /**
     * 目录
     */
    @GetMapping("/menu")
    public ApiReturnObject menu() {
        List<CatalogLive> liveList = catalogLiveService.selectCatalogLiveList(null);
        return new ApiReturnObject("200", "操作成功", liveList);
    }

    /**
     * 收藏
     */
    @PostMapping("/like")
    public ApiReturnObject like(@RequestBody JrUserConent userConent) {
        List<JrUserConent> conentList = userConentService.selectJrUserConentList(userConent);
        if (userConent.getUserId() == null || userConent.getComentId() == null){
            return new ApiReturnObject("201", "failure", "请先登录");
        }
        if (conentList.size() == 0) {
            userConent.setCreatetime(new Date());
            userConentService.insertJrUserConent(userConent);
            return new ApiReturnObject("200", "success", "收藏成功");
        } else {
            userConentService.deleteJrUserConentByIds(conentList.get(0).getId().toString());
            return new ApiReturnObject("200", "success", "取消收藏成功");
        }
    }

    /**
     * 我的收藏
     */
    @GetMapping("/isMyLike")
    public ApiReturnObject myLike(Integer userId,Integer pageNum,Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<JrMoments> jrMoments = momentsService.selectByLike(userId);
        for (JrMoments jrMoment : jrMoments) {
            Map<String, Object> map = userFeign.selectByUserId(Integer.parseInt(jrMoment.getUserId()));
            jrMoment.setSysIco((String) map.get("image"));
            jrMoment.setSysRelName((String) map.get("name"));
            String[] split = jrMoment.getImages().split(",");
            jrMoment.setImageList(split);
        }
        return new ApiReturnObject("200", "success", new PageInfo<JrMoments>(jrMoments));
    }

    /**
     * 相关视屏
     */
    @GetMapping("/relatedVideolist")
    public ApiReturnObject relatedVideolist(Integer catalogId, Integer videoId) {
        JrMoments jrMoments = new JrMoments();
        jrMoments.setCatalogId(catalogId);
        List<JrMoments> jrMoments1 = momentsService.selectJrMomentsList(jrMoments);
        JrUserConent jrUserConent = new JrUserConent();
        for (JrMoments moments : jrMoments1) {
            Map<String, Object> stringObjectMap = userFeign.selectByUserId(Integer.parseInt(moments.getUserId()));
            jrMoments.setUserName(stringObjectMap.get("name") + "");
            jrMoments.setUserIco(stringObjectMap.get("image") + "");
            //视屏id
            Integer id = moments.getId();
            String userId = moments.getUserId();
            jrUserConent.setComentId(id);
            jrUserConent.setUserId(Integer.parseInt(userId));
            List<JrUserConent> jrUserConents = jrUserConentService.selectJrUserConentList(jrUserConent);
            //0未收藏 1已收藏
            if(jrUserConents.size()!=0){
                moments.setIsLike("1");
            }else{
                moments.setIsLike("0");
            }
        }
        jrMoments1.removeIf(next -> next.getId().equals(videoId));
        if(jrMoments1.size()>=4){
            return new ApiReturnObject("200", "success", jrMoments1.subList(0, 4));
        }
        return new ApiReturnObject("200", "success", jrMoments1);
    }

}